﻿using System;
using System.Collections.Generic;
using KKSG;
using UnityEngine;
using XUtliPoolLib;

namespace XMainClient
{
	// Token: 0x02000FCF RID: 4047
	internal class PartLoadTask : EquipLoadTask
	{
		// Token: 0x0600D233 RID: 53811 RVA: 0x003103F4 File Offset: 0x0030E5F4
		public PartLoadTask(EPartType p, PartLoadCallback partLoadCb) : base(p)
		{
			this.loadCb = new LoadCallBack(this.LoadFinish);
			this.m_PartLoadCb = partLoadCb;
		}

		// Token: 0x0600D234 RID: 53812 RVA: 0x00310478 File Offset: 0x0030E678
		public override void Load(XEntity e, int prefessionID, ref FashionPositionInfo newFpi, bool async, HashSet<string> loadedPath)
		{
			bool flag = base.IsSamePart(ref newFpi);
			if (flag)
			{
				bool flag2 = this.m_PartLoadCb != null;
				if (flag2)
				{
					this.m_PartLoadCb(this, false);
				}
			}
			else
			{
				this.Reset(e);
				bool flag3 = base.MakePath(ref newFpi, loadedPath);
				if (flag3)
				{
					this.replaceMeshLocation = null;
					this.replaceTexLocation = null;
					bool meshInfo = XEquipDocument._MeshPartList.GetMeshInfo(this.location, prefessionID, (int)this.part, newFpi.fashionDir, out this.partType, ref this.replaceMeshLocation, ref this.replaceTexLocation);
					if (meshInfo)
					{
						if (async)
						{
							bool flag4 = this.replaceMeshLocation != null;
							if (flag4)
							{
								this.meshLoadTask = XSingleton<XResourceLoaderMgr>.singleton.GetShareResourceAsync<Mesh>(this.replaceMeshLocation, ".asset", this.loadCb, PartLoadTask.fakeMeshPart);
							}
							else
							{
								this.meshLoadTask = XSingleton<XResourceLoaderMgr>.singleton.GetShareResourceAsync<Mesh>(this.location, ".asset", this.loadCb, PartLoadTask.fakeMeshPart);
							}
							bool flag5 = this.replaceTexLocation != null;
							if (flag5)
							{
								this.texLoadTask = XSingleton<XResourceLoaderMgr>.singleton.GetShareResourceAsync<Texture2D>(this.replaceTexLocation, ".tga", this.loadCb, PartLoadTask.fakeTexPart);
								bool flag6 = this.partType == XMeshPartList.CutoutPart;
								if (flag6)
								{
									this.alphaLoadTask = XSingleton<XResourceLoaderMgr>.singleton.GetShareResourceAsync<Texture2D>(this.replaceTexLocation + "_A", ".png", this.loadCb, PartLoadTask.fakeAlphaTexPart);
								}
							}
							else
							{
								this.texLoadTask = XSingleton<XResourceLoaderMgr>.singleton.GetShareResourceAsync<Texture2D>(this.location, ".tga", this.loadCb, PartLoadTask.fakeTexPart);
								bool flag7 = this.partType == XMeshPartList.CutoutPart;
								if (flag7)
								{
									this.alphaLoadTask = XSingleton<XResourceLoaderMgr>.singleton.GetShareResourceAsync<Texture2D>(this.location + "_A", ".png", this.loadCb, PartLoadTask.fakeAlphaTexPart);
								}
							}
						}
						else
						{
							bool flag8 = this.replaceMeshLocation != null;
							Mesh sharedResource;
							if (flag8)
							{
								sharedResource = XSingleton<XResourceLoaderMgr>.singleton.GetSharedResource<Mesh>(this.replaceMeshLocation, ".asset", true, false);
							}
							else
							{
								sharedResource = XSingleton<XResourceLoaderMgr>.singleton.GetSharedResource<Mesh>(this.location, ".asset", true, false);
							}
							this.LoadFinish(sharedResource, PartLoadTask.fakeMeshPart);
							bool flag9 = this.replaceTexLocation != null;
							if (flag9)
							{
								Texture2D sharedResource2 = XSingleton<XResourceLoaderMgr>.singleton.GetSharedResource<Texture2D>(this.replaceTexLocation, ".tga", true, false);
								this.LoadFinish(sharedResource2, PartLoadTask.fakeTexPart);
								bool flag10 = this.partType == XMeshPartList.CutoutPart;
								if (flag10)
								{
									Texture2D sharedResource3 = XSingleton<XResourceLoaderMgr>.singleton.GetSharedResource<Texture2D>(this.replaceTexLocation + "_A", ".png", true, false);
									this.LoadFinish(sharedResource3, PartLoadTask.fakeAlphaTexPart);
								}
							}
							else
							{
								Texture2D sharedResource4 = XSingleton<XResourceLoaderMgr>.singleton.GetSharedResource<Texture2D>(this.location, ".tga", true, false);
								this.LoadFinish(sharedResource4, PartLoadTask.fakeTexPart);
								bool flag11 = this.partType == XMeshPartList.CutoutPart;
								if (flag11)
								{
									Texture2D sharedResource5 = XSingleton<XResourceLoaderMgr>.singleton.GetSharedResource<Texture2D>(this.location + "_A", ".png", true, false);
									this.LoadFinish(sharedResource5, PartLoadTask.fakeAlphaTexPart);
								}
							}
						}
					}
					else
					{
						this.processStatus = EProcessStatus.EPreProcess;
						bool flag12 = this.m_PartLoadCb != null;
						if (flag12)
						{
							this.m_PartLoadCb(this, true);
						}
					}
				}
				else
				{
					bool flag13 = this.m_PartLoadCb != null;
					if (flag13)
					{
						this.m_PartLoadCb(this, true);
					}
				}
			}
		}

		// Token: 0x0600D235 RID: 53813 RVA: 0x00310808 File Offset: 0x0030EA08
		private void LoadFinish(UnityEngine.Object obj, object cbOjb)
		{
			bool flag = this.processStatus == EProcessStatus.EProcessing;
			if (flag)
			{
				bool flag2 = cbOjb is PartLoadTask.MeshPart;
				if (flag2)
				{
					this.mesh = (obj as Mesh);
					this.loadState |= 1;
					this.meshLoadTask = null;
				}
				else
				{
					bool flag3 = cbOjb is PartLoadTask.TexPart;
					if (flag3)
					{
						Texture2D texture2D = obj as Texture2D;
						this.tex = texture2D;
						this.texLoadTask = null;
						this.loadState |= 2;
					}
					else
					{
						bool flag4 = cbOjb is PartLoadTask.AlphaTexPart;
						if (flag4)
						{
							Texture2D texture2D2 = obj as Texture2D;
							this.aplhaTex = texture2D2;
							this.alphaLoadTask = null;
							this.loadState |= 4;
						}
						else
						{
							XSingleton<XDebug>.singleton.AddErrorLog("assert error equip type", null, null, null, null, null);
						}
					}
				}
				bool flag5 = XMeshPartList.ConvertType(this.partType) == this.loadState;
				if (flag5)
				{
					this.processStatus = EProcessStatus.EPreProcess;
					bool flag6 = this.m_PartLoadCb != null;
					if (flag6)
					{
						this.m_PartLoadCb(this, true);
					}
				}
			}
		}

		// Token: 0x0600D236 RID: 53814 RVA: 0x00310924 File Offset: 0x0030EB24
		public override void Reset(XEntity e)
		{
			bool flag = this.mesh != null;
			if (flag)
			{
				bool flag2 = !string.IsNullOrEmpty(this.replaceMeshLocation);
				if (flag2)
				{
					XSingleton<XResourceLoaderMgr>.singleton.UnSafeDestroyShareResource(this.replaceMeshLocation, ".asset", this.mesh, XSingleton<XScene>.singleton.SceneType == SceneType.SCENE_HALL);
				}
				else
				{
					XSingleton<XResourceLoaderMgr>.singleton.UnSafeDestroyShareResource(this.location, ".asset", this.mesh, XSingleton<XScene>.singleton.SceneType == SceneType.SCENE_HALL);
				}
				this.mesh = null;
			}
			this.replaceMeshLocation = null;
			bool flag3 = this.tex != null;
			if (flag3)
			{
				bool flag4 = !string.IsNullOrEmpty(this.replaceTexLocation);
				if (flag4)
				{
					XSingleton<XResourceLoaderMgr>.singleton.UnSafeDestroyShareResource(this.replaceTexLocation, ".tga", this.tex, XSingleton<XScene>.singleton.SceneType == SceneType.SCENE_HALL);
				}
				else
				{
					XSingleton<XResourceLoaderMgr>.singleton.UnSafeDestroyShareResource(this.location, ".tga", this.tex, XSingleton<XScene>.singleton.SceneType == SceneType.SCENE_HALL);
				}
				this.tex = null;
			}
			bool flag5 = this.aplhaTex != null;
			if (flag5)
			{
				XSingleton<XResourceLoaderMgr>.singleton.UnSafeDestroyShareResource(this.location + "_A", ".png", this.aplhaTex, XSingleton<XScene>.singleton.SceneType == SceneType.SCENE_HALL);
				this.aplhaTex = null;
			}
			this.replaceTexLocation = null;
			this.loadState = 0;
			bool flag6 = this.meshLoadTask != null;
			if (flag6)
			{
				this.meshLoadTask.CancelLoad(this.loadCb);
				this.meshLoadTask = null;
			}
			bool flag7 = this.texLoadTask != null;
			if (flag7)
			{
				this.texLoadTask.CancelLoad(this.loadCb);
				this.texLoadTask = null;
			}
			bool flag8 = this.alphaLoadTask != null;
			if (flag8)
			{
				this.alphaLoadTask.CancelLoad(this.loadCb);
				this.alphaLoadTask = null;
			}
			base.Reset(e);
		}

		// Token: 0x0600D237 RID: 53815 RVA: 0x00310B24 File Offset: 0x0030ED24
		public bool HasMesh()
		{
			return this.mesh != null;
		}

		// Token: 0x0600D238 RID: 53816 RVA: 0x00310B44 File Offset: 0x0030ED44
		public Texture2D GetTexture()
		{
			return this.tex;
		}

		// Token: 0x0600D239 RID: 53817 RVA: 0x00310B5C File Offset: 0x0030ED5C
		public Texture2D GetAlpha()
		{
			return this.aplhaTex;
		}

		// Token: 0x04005F71 RID: 24433
		private string replaceMeshLocation = null;

		// Token: 0x04005F72 RID: 24434
		private string replaceTexLocation = null;

		// Token: 0x04005F73 RID: 24435
		public byte partType = 0;

		// Token: 0x04005F74 RID: 24436
		public Mesh mesh = null;

		// Token: 0x04005F75 RID: 24437
		private Texture2D tex = null;

		// Token: 0x04005F76 RID: 24438
		private Texture2D aplhaTex = null;

		// Token: 0x04005F77 RID: 24439
		private byte loadState = 0;

		// Token: 0x04005F78 RID: 24440
		private static PartLoadTask.MeshPart fakeMeshPart = new PartLoadTask.MeshPart();

		// Token: 0x04005F79 RID: 24441
		private static PartLoadTask.TexPart fakeTexPart = new PartLoadTask.TexPart();

		// Token: 0x04005F7A RID: 24442
		private static PartLoadTask.AlphaTexPart fakeAlphaTexPart = new PartLoadTask.AlphaTexPart();

		// Token: 0x04005F7B RID: 24443
		private LoadAsyncTask meshLoadTask = null;

		// Token: 0x04005F7C RID: 24444
		private LoadAsyncTask texLoadTask = null;

		// Token: 0x04005F7D RID: 24445
		private LoadAsyncTask alphaLoadTask = null;

		// Token: 0x04005F7E RID: 24446
		private LoadCallBack loadCb = null;

		// Token: 0x04005F7F RID: 24447
		private PartLoadCallback m_PartLoadCb = null;

		// Token: 0x020019F8 RID: 6648
		private class MeshPart
		{
		}

		// Token: 0x020019F9 RID: 6649
		private class TexPart
		{
		}

		// Token: 0x020019FA RID: 6650
		private class AlphaTexPart
		{
		}
	}
}
